/**
 *  chdev.c - Related calls for char device driver
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "drivers/chdev.h"
#include "utility/bmisc.h"


/**
 * chdevRegister - Register a device to Bycore.
 * @Mid: Main ID.
 * @Sid: Secondary ID.
 * @Name: Device name.
 * @Operation: Pointer to operations of device.
 * @return: DEV_OK if successful, otherwise reasons about failure.
 *
 * @notes:
 */
int32 chdevRegister(uint8 Mid, uint8 Sid, const char_t *Name,
                    uint32 Size, struct chdev_operations *Operation)
{
    chdev_t *pchdev = NULL;
    int32 result = FALSE;

    if((Name == NULL) || (Operation == NULL)) {
        return DEV_FALSE;
    }

    if((pchdev = (chdev_t*)kmalloc(sizeof(chdev_t))) == NULL) {
        return DEV_NO_MEM;
    }

    pchdev->op = Operation;
    pchdev->loff = 0;
    pchdev->capacity = Size;
    result = char_device_register(&pchdev->dev, Name, Mid, Sid,
                                  Operation->release);

    if(result == DEV_FALSE) {
        kfree(pchdev);
    }

    return result;
}



/**
 * chdevUnRegister - Unregister a device from Bycore.
 * @Mid: Main ID.
 * @Sid: Secondary ID.
 *
 * @notes:
 */
void chdevUnRegister(uint32 chdevMID, uint32 chdevSID)
{
    char_device_unregister(chdevMID, chdevSID);
}



/**
 * CDevOpen -
 * @Self:
 *
 * @notes:
 */
chdev_t *CDevOpen(const char_t *name)
{
    chdev_t *dev = NULL;

    if(name == NULL) {
        return(NULL);
    }

    dev = (chdev_t*)device_open(name);
    if(dev == NULL) {
        return(NULL);
    }

    dev->loff = 0;

    if((dev->op != NULL) && (dev->op->open != NULL))
    {
        device_lock((device_t*)dev);
        dev->op->open();
        device_unlock((device_t*)dev);
    }

    return(dev);
}



/**
 * CDevClose -
 * @Self:
 *
 * @notes:
 */
int32 CDevClose(chdev_t *Self)
{
    int32 ret = -1;

    if(Self == NULL) {
        return(ret);
    }

    if((Self->op != NULL) && (Self->op->close != NULL)) {
        device_lock((device_t*)Self);
        ret = Self->op->close();
        device_unlock((device_t*)Self);
    }
    device_close((device_t*)Self);

    return(ret);
}



/**
 * CDevRead -
 * @Self:
 * @LOff:
 *
 * @notes:
 */
loff_t CDevLLSeek(chdev_t *Self, loff_t LOff)
{
    loff_t loff = 0;

    if(Self == NULL) {
        return(loff);
    }

    device_lock((device_t*)Self);
    loff = Self->loff;
    Self->loff = LOff;
    device_unlock((device_t*)Self);

    return(loff);
}



/**
 * CDevRead -
 * @Self:
 * @Buf:
 * @Len:
 *
 * @notes:
 */
int32 CDevWrite(chdev_t *Self, const char_t *Buf, uint32 Len)
{
    uint32 length = 0;

    if((Self == NULL) || (Buf == NULL))
    {
        return(length);
    }

    if((Self->op != NULL) && (Self->op->write != NULL)) {
        device_lock((device_t*)Self);

        length = Self->op->write(Buf, Len, Self->loff);
        Self->loff += length;
        Self->loff = bmin(Self->loff, Self->capacity);

        device_unlock((device_t*)Self);
    }

    return(length);
}



/**
 * CDevRead -
 * @Self:
 * @Buf:
 * @Len:
 *
 * @notes:
 */
int32 CDevRead(chdev_t *Self, char_t *Buf, uint32 Len)
{
    uint32 length = Len;

    if((Self == NULL) || (Buf == NULL)) {
        return(0);
    }

    if((Self->op != NULL) && (Self->op->read != NULL)) {
        device_lock((device_t*)Self);

        length = Self->op->read(Buf, Len, Self->loff);
        Self->loff += length;
        Self->loff = bmin(Self->loff, Self->capacity);

        device_unlock((device_t*)Self);
    }

    return(length);
}



/**
 * CDevIoctl -
 * @Self:
 * @Cmd:
 * @Arg:
 *
 * @notes:
 */
int32 CDevIoctl(chdev_t *Self, uint32 Cmd, void *Arg)
{
    int32 ret = 0;

    if(Self == NULL) {
        return(0);
    }

    if((Self->op != NULL) && (Self->op->ioctl != NULL)) {
        device_lock((device_t*)Self);
        ret = Self->op->ioctl(Cmd, Arg);
        device_unlock((device_t*)Self);
    }

    return ret;
}

